<?php
/**
 * 注：此模板为自动生成模板，需要根据实际情况调整修改
 * 此模板对 无主键/单一主键 表的支持较好，联合主键表在下一版本开发
 */
namespace Data;

use Utils\MyException;

class Order_form_DAO {

    public static $TABLENAME = 'order_form';

    public static $_db;

    public static function query($str)
    {
        if(self::$_db === null) {
            self::$_db = \GatewayWorker\Lib\Db::instance('main');
            return self::$_db->query($str);
        } else {
            return self::$_db->query($str);
        }
    }




    public static function getRecord($ID){
        $where = "`ORDER_FORM_ID` = '$ID'";
        $record = self::query('select * from '.static::$TABLENAME .' where '.$where);
        if($record != null){
            return new Order_form($record[0]);
        }
        return null;
    }



    public static function getList() {
        $where = "";
        $list = self::query('select * from '.static::$TABLENAME);
        $results = array();

        foreach($list as $record){
            $results[] = new Order_form($record);
        }
        return $results;
    }


    public static function getNotEntryList($restaurant_id,$list_start,$every_page_counts) {
        $where = "`RESTAURANT_ID`='$restaurant_id' AND `IS_ENTRY`=".STATE_NO." AND `PAY_STATUS`=".PAY_STATUS_YES." AND (`PAY_WAY`=".PAY_WAY_WX." OR `PAY_WAY`=".PAY_WAY_ZFB.") ORDER BY `UPDATED_ON` DESC limit $list_start,$every_page_counts";
        $list = self::query('select `TOTAL_PRICE`,`TOTAL_PRICE_LEFT`,`UPDATED_ON` from '.static::$TABLENAME.' where '.$where);

        return $list;
    }

    public static function getEntryList($restaurant_id,$list_start,$every_page_counts) {
        $where = "`RESTAURANT_ID`='$restaurant_id' AND `IS_ENTRY`=".STATE_YES." ORDER BY `UPDATED_ON` DESC limit $list_start,$every_page_counts";
        $list = self::query('select `TOTAL_PRICE`,`TOTAL_PRICE_LEFT`,`UPDATED_ON` from '.static::$TABLENAME.' where '.$where);

        return $list;
    }



    public static function getListByResIdAndDate($restaurant_id,$date,$pay_status,$last_id,$every_page_counts,$table_number,$order_num) {
        $where = "`RESTAURANT_ID`='$restaurant_id' AND `DATE`='$date' AND `PAY_STATUS`='$pay_status'";
        if ($pay_status == PAY_STATUS_YES) {
            $where .= " AND `RETREAT_ORDER_STATUS`!=".RETREAT_ORDER_STATUS_SUC;
        }
        if ($last_id) {
            $where .= " AND `ORDER_FORM_ID`<'$last_id'";
        }
        if ($table_number) {
            $where .= " AND `TABLE_NUM`='$table_number'";
        }
        if ($order_num) {
            $where .= " AND `ORDER_NUM`='$order_num'";
        }
        $where .= " ORDER BY `UPDATED_ON` limit $every_page_counts";

        $list = self::query('select * from '.static::$TABLENAME.' where '.$where);
        $results = array();

        foreach($list as $record){
            $results[] = new Order_form($record);
        }
        return $results;
    }

    public static function getListRetreat($restaurant_id,$date,$pay_status,$last_id,$every_page_counts,$table_number,$order_num) {
        $where = "`RESTAURANT_ID`='$restaurant_id' AND `DATE`='$date' AND `PAY_STATUS`='$pay_status'";

        $where .= " AND (`RETREAT_ORDER_STATUS`=".RETREAT_ORDER_STATUS_SUC." OR `RETREAT_FOOD_STATUS`=".RETREAT_FOOD_STATUS_SUC.")";

        if ($last_id) {
            $where .= " AND `ORDER_FORM_ID`<'$last_id'";
        }
        if ($table_number) {
            $where .= " AND `TABLE_NUM`='$table_number'";
        }
        if ($order_num) {
            $where .= " AND `ORDER_NUM`='$order_num'";
        }
        $where .= " ORDER BY `UPDATED_ON` limit $every_page_counts";
var_dump('select * from '.static::$TABLENAME.' where '.$where);
        $list = self::query('select * from '.static::$TABLENAME.' where '.$where);
        $results = array();

        foreach($list as $record){
            $results[] = new Order_form($record);
        }
        return $results;
    }

    public static function getListReminder($restaurant_id,$date,$reminder_status,$last_id,$every_page_counts,$table_number,$order_num) {
        $where = "`RESTAURANT_ID`='$restaurant_id' AND `DATE`='$date' AND `REMINDER_STATUS`='$reminder_status'";
        if ($last_id) {
            $where .= " AND `ORDER_FORM_ID`<'$last_id'";
        }
        if ($table_number) {
            $where .= " AND `TABLE_NUM`='$table_number'";
        }
        if ($order_num) {
            $where .= " AND `ORDER_NUM`='$order_num'";
        }
        $where .= " ORDER BY `UPDATED_ON` limit $every_page_counts";
        $list = self::query('select * from '.static::$TABLENAME.' where '.$where);
        $results = array();

        foreach($list as $record){
            $results[] = new Order_form($record);
        }
        return $results;
    }


    public static function getListRetreatFood($restaurant_id,$date,$retreat_food_status,$last_id,$every_page_counts,$table_number,$order_num) {
        $where = "`RESTAURANT_ID`='$restaurant_id' AND `DATE`='$date' AND `RETREAT_FOOD_STATUS`='$retreat_food_status'";
        if ($last_id) {
            $where .= " AND `ORDER_FORM_ID`<'$last_id'";
        }
        if ($table_number) {
            $where .= " AND `TABLE_NUM`='$table_number'";
        }
        if ($order_num) {
            $where .= " AND `ORDER_NUM`='$order_num'";
        }
        $where .= " ORDER BY `UPDATED_ON` limit $every_page_counts";
        $list = self::query('select * from '.static::$TABLENAME.' where '.$where);
        $results = array();

        foreach($list as $record){
            $results[] = new Order_form($record);
        }
        return $results;
    }


    public static function getListRetreatOrder($restaurant_id,$date,$retreat_order_status,$last_id,$every_page_counts,$table_number,$order_num) {
        $where = "`RESTAURANT_ID`='$restaurant_id' AND `DATE`='$date' AND `RETREAT_ORDER_STATUS`='$retreat_order_status'";
        if ($last_id) {
            $where .= " AND `ORDER_FORM_ID`<'$last_id'";
        }
        if ($table_number) {
            $where .= " AND `TABLE_NUM`='$table_number'";
        }
        if ($order_num) {
            $where .= " AND `ORDER_NUM`='$order_num'";
        }
        $where .= " ORDER BY `UPDATED_ON` limit $every_page_counts";
        $list = self::query('select * from '.static::$TABLENAME.' where '.$where);
        $results = array();

        foreach($list as $record){
            $results[] = new Order_form($record);
        }
        return $results;
    }

    public static function getListByConsumerId($consumer_id,$list_start,$every_page_counts,$restaurant_id) {
        $where = "`CREATED_BY`='$consumer_id' ";
        if ($restaurant_id) {
            $where .= " AND `RESTAURANT_ID`='$restaurant_id'";
        }
        $where .= " ORDER BY `CREATED_ON` DESC limit $list_start,$every_page_counts";

        $list = self::query('select * from '.static::$TABLENAME.' where '.$where);
        $results = array();

        foreach($list as $record){
            $results[] = new Order_form($record);
        }
        return $results;
    }

    public static function getSumTotalPrice($restaurant_id,$yesterday_date) {
        $where = "`RESTAURANT_ID`='$restaurant_id' AND `DATE`='$yesterday_date' AND `PAY_STATUS`=".STATE_YES;
        $list = self::query('select SUM(CONSUMER_COUNT) AS '.CUSUMOR_COUNT.' ,COUNT(*) AS '.TOTAL_FOOD_COUNT.',SUM(`TOTAL_PRICE`) AS '.TURNOVER.' from '.static::$TABLENAME.' where '.$where);
        return $list[0];
    }


    public static function getSumOrderPrice($restaurant_id,$yesterday_date) {
        $where = "`RESTAURANT_ID`='$restaurant_id' AND `DATE`='$yesterday_date' AND `PAY_STATUS`=".STATE_YES;
        $list = self::query('select SUM(CONSUMER_COUNT) AS '.CUSUMOR_COUNT.' ,COUNT(*) AS '.TOTAL_FOOD_COUNT.',SUM(`TOTAL_PRICE`) AS '.TURNOVER.' from '.static::$TABLENAME.' where '.$where);
        return $list[0];
    }

    public static function getSumOrderPriceNotEntry($restaurant_id) {
        $where = "`RESTAURANT_ID`='$restaurant_id' AND `IS_ENTRY`=".STATE_NO." AND `PAY_STATUS`=".PAY_STATUS_YES." AND (`PAY_WAY`=".PAY_WAY_WX." OR `PAY_WAY`=".PAY_WAY_ZFB.")";
        $list = self::query('select SUM(`TOTAL_PRICE`) AS '.ACCOUNT_NOT_ENTRY.' from '.static::$TABLENAME.' where '.$where);
        if (!$list[0][ACCOUNT_NOT_ENTRY]) {
            $list[0][ACCOUNT_NOT_ENTRY]=0;
        }
        return $list[0][ACCOUNT_NOT_ENTRY];
    }

    public static function insert($record){

        try {
            $ID = $record->getORDER_FORM_ID();
            $oldrecord = self::getRecord($ID);
            if($oldrecord != null) {
                throw new MyException ("重复插入");
            }
            $sql = 'insert into '.static::$TABLENAME .' values ('.$record->toInsertCSV().')' ;
            self::query($sql);
            return self::$_db->lastInsertId();
        } catch(MyException $e){
            throw $e;
        }
    }



    public static function update($record){
        try{
            $ID = $record->getORDER_FORM_ID();
            $where = "`ORDER_FORM_ID` = '$ID'";
            $oldrecord = self::getRecord($ID);
            if($oldrecord == null){
                throw new MyException("不存在该记录");
            }
            $sql = 'update '.static::$TABLENAME .' set '.$record->toUpdateSet().' where '.$where ;
            self::query($sql);
        } catch(MyException $e){
            throw $e;
        }
    }



    public static function delete ($record){
        try{
            $ID = $record->getORDER_FORM_ID();
            $where = "`ORDER_FORM_ID` = '$ID'";
            $oldrecord = self::getRecord($ID);
            if($oldrecord == null){
                throw new MyException("不存在该记录");
            }
            $sql = 'delete from '.static::$TABLENAME . ' where '.$where ;
            self::query($sql);
        } catch(MyException $e){
            throw $e;
        }
    }
}
